#pragma once

#include <fstream>
#include <list>
#include <vector>
#include <string.h>
#include <qbprotocol/include/SSQBModel.h>
#include "BestQuoteMiniFilter.h"

struct PRICE2 {
    char			m_bp_status[4];					// 订单状态 \n-1：待审核 \n0：发布\n1：成交\n2：作废
    char			m_price_status[3];				// 价格状态，0:报价被refer或deal掉了 1:报价发布或者报价删除或者内部报价
    char			m_clean_price[12];				// 净价
    char			m_yield_price[12];				// 收益率
    char			m_sPrice[12];					// 价
    char			m_quotetype[4];					// 报价类型，0-有意向；1-净价；2-全价；3-收益率；4-利差
    char			m_sVolume[64];					// 量
    char			m_description[128];				// 价格备注
    char			m_interpret[128];				// 拆解后的报价，格式(T+0,2000;T+1,2000;远期,2000)，Add by Lance, 2021/09/10
    char			m_exercise[2];					// '0'-行权，'1'-到期
    time_t			m_create_time;					// 创建时间
    time_t			m_time;							// 更新时间
    PRICE2() {
        memset(this, 0, sizeof(PRICE2));
    };
public:
    void SetPrice(xQBBestPriceUnit_c& unit);
    void SetPrice(PRICE2& price);
    bool IsPriceStatusValid() const {
        //价格状态只要不是0或者空即算有效
        return (strcmp(m_price_status, "") == 0 || strcmp(m_price_status, "0") == 0) ? false : true;
    }
};

struct REPORT2 {
    unsigned long	m_dwVersion;				// 报价版本号
// 	DWORD	        m_dwMarket;					// 证券市场类型,see CBondData::StockMarket
    char			m_sBondKey[33];				// 债券编码
    char			m_sListedMarket[8];			// 市场代码
    time_t			m_time;						// 更新时间
    time_t			m_create_time;				// 创建时间
//	char	        m_bp_status[4];				// m_deal_status, 与成交区分，故改为bp_status, 订单状态 \n-1：待审核 \n0：发布\n1：成交\n2：作废
    char			m_company_id[33];			// 经纪公司ID
//	char	        m_price_status[3];			// 价格状态，0:报价被refer或deal掉了 1:报价发布或者报价删除或者内部报价(0表示无效报价，非0表示有效报价)
    PRICE2	        m_bidinfo;					// bid信息
    PRICE2	        m_askinfo;					// ask信息
    int8_t          m_dir;                      // 方向
public:
    bool IsPriceStatusValid() const {
        // bid/ask两边报价状态只要一个有效就算有效
        return m_bidinfo.IsPriceStatusValid() || m_askinfo.IsPriceStatusValid();
    }
};

struct BondIndexBroker
{
public:
    unsigned int brokerId;
    unsigned int bondIndex;
    unsigned int reportIndex;

public:
    BondIndexBroker() :brokerId(0), bondIndex(0){};
    BondIndexBroker(int bk, int bi, int rpt)
    {
        brokerId = bk;
        bondIndex = bi;
        reportIndex = rpt;
    }

    BondIndexBroker& operator = (const BondIndexBroker& bk)
    {
        brokerId = bk.brokerId;
        bondIndex = bk.bondIndex;
        reportIndex = bk.reportIndex;
        return *this;
    }
    bool operator == (const BondIndexBroker& bk)
    {
        return brokerId == bk.brokerId && bondIndex == bk.bondIndex;
    }
};

struct BestQuoteProcessData
{
    BestQuoteProcessData() : pbib(NULL), prpt(NULL), count(0) {}

    BondIndexBroker* pbib;
    REPORT2*          prpt;
    unsigned int     count;
};

enum eBestQuoteSortKey {
    eSort_None = 0,
    eSort_Code,
    eSort_Name,
    eSort_VolBid,
    eSort_Bid,
    eSort_Ofr,
    eSort_VolOfr,
    eSort_Rate,
    eSort_Broker,
    eSort_time,
    eSort_count
};

class BestQuoteData
{
public:
    BestQuoteData();
    ~BestQuoteData();

    static BestQuoteData& GetInstance();

public:
    BestQuoteMiniFilter& GetFilter();
    void    InitData();
    void	UpdateData(void* pData);
    void    UnInitData();
    void    ReloadData();
    void	Sort();
    void	SetSortKey(eBestQuoteSortKey sortKey) { m_sortKey = sortKey; };
    eBestQuoteSortKey GetSortKey() { return m_sortKey; };
    void	SetSortDirection(bool sortDirection) { m_sortDirection = sortDirection; };
    bool	GetSortDirection() { return m_sortDirection; };

    bool    IsPassCondition(const BondIndexBroker& bk);
public:
    std::vector<REPORT2>			m_quotedata;
    std::vector<BondIndexBroker>	m_index;
	std::vector<int>                m_result;

private:
	BestQuoteMiniFilter             m_filter;
    int32_t**                       m_pBondRptIndex;    //brokerId + bondindex，作为报价唯一标识
    eBestQuoteSortKey               m_sortKey;
	bool                            m_sortDirection;
};
